package com.gxuwz.com_property.notice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxuwz.com_property.common.dto.ResponseDto;
import com.gxuwz.com_property.notice.dto.NoticeDto;
import com.gxuwz.com_property.notice.pojo.Notice;
import com.gxuwz.com_property.notice.service.NoticeService;
import com.gxuwz.com_property.utils.RamdomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@RestController
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 随机数
     * */
    @Autowired
    private RamdomUtils ramdomUtils;

    /**
     * 添加公告
     */
    @PostMapping("/addNoticeInfo")
    public ResponseDto addNoticeInfo(@RequestBody Notice notice) {

        long repair_id = ramdomUtils.generateId();

        notice.setId(repair_id);
        /**
         * 设置时间格式
         * */
        if (notice.getPublishTime()!=null){
            // 原始日期时间值
            String originalDateTime = notice.getPublishTime();
            // 将原始日期时间转换为 LocalDateTime 对象，并调整时区（这里假设您想要转换为系统默认时区）
            LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.parse(originalDateTime), ZoneId.systemDefault());
            // 将 LocalDateTime 对象转换为符合 MySQL 的日期时间格式
            DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String mysqlDateTime = dtf.format(localDateTime);
            notice.setPublishTime(mysqlDateTime);
        }

        Boolean result = noticeService.addNoticeInfo(notice);

        if (result) {
            return ResponseDto.success("添加公告成功");
        } else {
            return ResponseDto.error("添加公告失败");
        }
    }

    /**
     * 修改公告
     */
    @PostMapping("/updateNoticeInfo")
    public ResponseDto updateNoticeInfo(@RequestBody Notice notice) {

        /**
         * 设置时间格式
         */
        if (notice.getPublishTime() != null) {
            // 原始日期时间值
            String originalDateTime = notice.getPublishTime();
            // 使用正确的日期时间格式解析原始日期时间字符串
            DateTimeFormatter inputDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime localDateTime = LocalDateTime.parse(originalDateTime, inputDtf);
            // 将 LocalDateTime 对象转换为符合 MySQL 的日期时间格式
            DateTimeFormatter outputDtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String mysqlDateTime = outputDtf.format(localDateTime);
            notice.setPublishTime(mysqlDateTime);
        }

        Boolean result = noticeService.updateNoticeInfo(notice);

        if (result) {
            return ResponseDto.success("修改公告成功");
        } else {
            return ResponseDto.error("修改公告失败");
        }
    }

    /**
     * 删除公告
     */
    @PostMapping("/deleteNoticeById")
    public ResponseDto deleteNoticeById(int id) {
        Boolean result = noticeService.deleteNoticeByIdInfo(id);

        if (result) {
            return ResponseDto.success("删除公告成功");
        } else {
            return ResponseDto.error("删除公告失败");
        }
    }

    /**
     * 根据ID查询公告
     */
    @PostMapping("/findNoticeByNotice")
    public ResponseDto findNoticeByNotice(Notice notice) {

        Notice noticeByNotice = noticeService.findNoticeByNotice(notice);


        if (noticeByNotice != null) {
            return ResponseDto.success("查询公告成功", noticeByNotice);
        } else {
            return ResponseDto.error("查询公告失败", noticeByNotice);
        }
    }

    /**
     * 分页查询公告
     */
    @PostMapping("/selectNoticesByPage")
    public ResponseDto selectNoticeByPage(@RequestBody NoticeDto noticeDto) {
        Integer pageNo = noticeDto.getPageNo();
        Integer pageSize = noticeDto.getPageSize();

        IPage<Notice> noticeIPage = noticeService.selectRepairByPage(new Page<>(pageNo, pageSize), noticeDto);

        if (noticeIPage != null) {
            return ResponseDto.success("查询公告成功", noticeIPage);
        } else {
            return ResponseDto.error("查询公告失败", noticeIPage);
        }
    }

}
